<?PHP
include("settings.php");
include("connect.php");

if ($_GET['type'] == 'query') {
     $query = "
	 SELECT DISTINCT `ownerName1` AS name FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'journaltable']."` WHERE `ownerName1` REGEXP '".mysql_real_escape_string($_GET['typing'])."'
	 UNION
	 SELECT DISTINCT `ownerName2` AS name FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'journaltable']."` WHERE `ownerName2` REGEXP '".mysql_real_escape_string($_GET['typing'])."'
	 UNION
	 SELECT DISTINCT `argName1` AS name FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'journaltable']."` WHERE `argName1` REGEXP '".mysql_real_escape_string($_GET['typing'])."' ORDER BY `name`
	 ";
    $result = $db->query($query);
	if ($db->foundRows > 0) {
    while ($row = $db->fetch_assoc($result)) {
        echo "
		<div onselect=\"this.text.value='".$row['name']."';\">
		".$row['name']."
		</div>";
    }
	} elseif ($db->foundRows == 0) {
        echo "<div>Oops, nothing found.</div>";
    } 
} else {
    $query = "SELECT DISTINCT `".mysql_real_escape_string($_GET['type'])."Name`, `".$_GET['type']."ID` FROM `".$settings[mysql_real_escape_string($_GET['wallet']).'transactionstable']."` WHERE `".mysql_real_escape_string($_GET['type'])."Name` REGEXP '".mysql_real_escape_string($_GET['typing'])."' ORDER BY `".mysql_real_escape_string($_GET['type'])."Name` ASC";
    $result = $db->query($query);
	if ($db->foundRows > 0) {
    while ($row = $db->fetch_assoc($result)) {
        echo "
		<div onselect=\"this.text.value='".$row[$_GET[type]."Name"]."'; document.getElementsByName('".$_GET['type']."ID')[0].value = '".$row[$_GET[type]."ID"]."';\">
		".$row[$_GET[type]."Name"]."
		</div>";
    }
	} elseif ($db->foundRows == 0) {
        echo "<div>Oops, nothing found.</div>";
    } 
}
?>